package com.example.dao;

import com.example.pojo.Student;
import com.example.sqlSession.SqlSession;
import com.example.sqlSession.SqlSessionFactory;
import com.example.sqlSession.SqlSessionFactoryBuilder;
import com.example.test.Resources;
import org.dom4j.DocumentException;

import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

public class StudentDaoIpml implements IstudentDao {


    public List<Student> findAll() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory builder = new SqlSessionFactoryBuilder().builder(resourceAsStream);
        SqlSession sqlSession = builder.openSession();

        List<Student> objects = sqlSession.selectList("student.selectList");
        for(Student stu :objects ){
            System.out.println(stu.getId()+"==="+stu.getName());
        }

        return objects;

    }

    public Student findByCondition(Student stu) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory builder = new SqlSessionFactoryBuilder().builder(resourceAsStream);
        SqlSession sqlSession = builder.openSession();

        Student student1 = sqlSession.selectOne("student.selectOne", stu);
        System.out.println(student1.getId()+"==="+student1.getName());

        return student1;

    }
}
